package CoteClient;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.SoftBevelBorder;

public class VueMenu extends Vue {

	private JPanel contentPane;
	private JPanel panelProfil;
	private JTextField textFieldNCompte;
	private JTextField textFieldOrg;
	private JTextField textFieldLangue;
	private JTextArea textAreaOrganisme;
	private JRadioButton rdbtnOccupe;
	private JRadioButton rdbtnDispo;
	private DefaultListModel model;
	private DefaultListModel modelCo;
	private JScrollPane scrollPane;
	private JList listCo;
	private Controleur controleur;
	private JTable table;
	private JLabel lblpseudo;
	private JTextField textFieldPseudo;
	private JTextField textFieldOrganismeLoc;
	private ButtonGroup g = new ButtonGroup(); // Sert à regrouper les boutons
												// entre et n'en laisser qu'un
												// selectionnable.

	public VueMenu(Controleur controleur) {
		super(controleur);
		this.controleur = controleur;
		System.out.println(this.controleur.getVec());
		setTitle("Menu");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 548, 419);
				
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnErim = new JMenu("Menu");
		menuBar.add(mnErim);
		
		JMenuItem mntmAjouterContact = new JMenuItem("ajouter Contact");
		mnErim.add(mntmAjouterContact);
		
		JMenuItem mntmQuitter = new JMenuItem("Quitter");
		mnErim.add(mntmQuitter);
		
//		JMenu mnStatut = new JMenu("Statut");
//		menuBar.add(mnStatut);
//		
//		
//		
//		
//		JMenuItem mntmDisponible = new JMenuItem("Disponible");
//		mnStatut.add(mntmDisponible);
//		mntmDisponible.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
////				getControleur().
//			}
//			});
//		
//		JMenuItem mntmOccup = new JMenuItem("Occupé");
//		mnStatut.add(mntmOccup);
		
		JMenu mnModifierProfil = new JMenu("Modifier Profil");
		menuBar.add(mnModifierProfil);
		
		JMenuItem mntmModifierLangues = new JMenuItem("Modifier Langues");
		mnModifierProfil.add(mntmModifierLangues);
		
		JMenuItem mntmModifierMotDe = new JMenuItem("Modifier Mot De Passe");
		mnModifierProfil.add(mntmModifierMotDe);
		mntmModifierMotDe.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().modifMdp();
			}
		});
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		////////////////////////////////////////////////////////////////////////////////////
		//JScrollPane scrollPane = new JScrollPane();
		//scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		
		//scrollPane.setBounds(12, 12, 158, 311);
		//contentPane.add(scrollPane);
		
		modelCo = new DefaultListModel();
		listCo = new JList(modelCo);
		listCo.setBounds(1, 1, 155, 284);
		listCo.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, new Color(0, 0, 0), null, null, null));
		listCo.setSelectionMode(0);
		contentPane.add(listCo);
		listCo.setCellRenderer(new CellRender());
		listCo.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				getControleur().infContact(listCo.getSelectedValue().toString());
//				listCo.setSelectionBackground(Color.getHSBColor(56, 143, 188));
			}
		});
	
		
		
		scrollPane = new JScrollPane(listCo);
		scrollPane.setBounds(12, 12, 158, 274);
		contentPane.add(scrollPane);
		 
		
		//list = new JList(alimente());
		//scrollPane.setViewportView(list);
		
		panelProfil = new JPanel();
		panelProfil.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panelProfil.setBackground(UIManager.getColor("Button.select"));
		panelProfil.setBounds(182, 110, 342, 213);
		contentPane.add(panelProfil);
		panelProfil.setLayout(null);
		
		JLabel lblNomDeCompte = new JLabel("Nom de compte");
		lblNomDeCompte.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNomDeCompte.setBounds(12, 12, 98, 15);
		panelProfil.add(lblNomDeCompte);
		
		JLabel lblOrganisme = new JLabel("Organisme");
		lblOrganisme.setHorizontalAlignment(SwingConstants.RIGHT);
		lblOrganisme.setBounds(22, 39, 88, 15);
		panelProfil.add(lblOrganisme);
		
		JLabel lblLangue = new JLabel("Langue");
		lblLangue.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLangue.setBounds(49, 66, 61, 15);
		panelProfil.add(lblLangue);
		
		textFieldNCompte = new JTextField();
		textFieldNCompte.setBackground(SystemColor.text);
		textFieldNCompte.setEditable(false);
		textFieldNCompte.setBounds(128, 10, 114, 19);
		panelProfil.add(textFieldNCompte);
		textFieldNCompte.setColumns(10);
		
		textFieldOrg = new JTextField();
		textFieldOrg.setBackground(SystemColor.text);
		textFieldOrg.setEditable(false);
		textFieldOrg.setBounds(128, 37, 114, 19);
		panelProfil.add(textFieldOrg);
		textFieldOrg.setColumns(10);
		
		textFieldLangue = new JTextField();
		textFieldLangue.setBackground(SystemColor.text);
		textFieldLangue.setEditable(false);
		textFieldLangue.setBounds(128, 64, 114, 19);
		panelProfil.add(textFieldLangue);
		textFieldLangue.setColumns(10);
		
		JButton btnAppeler = new JButton("Appeler");
		btnAppeler.setFont(new Font("Dialog", Font.BOLD, 11));
		btnAppeler.setBounds(183, 335, 102, 25);
		btnAppeler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//getControleur().lancerDiscussionLocuteur();
				getControleur().LancerCom((String)listCo.getSelectedValue());
				}
		});
		contentPane.add(btnAppeler);
		
		JButton btnDeconnexion = new JButton("Deconnexion");
		btnDeconnexion.setFont(new Font("Dialog", Font.BOLD, 11));
		btnDeconnexion.setBounds(421, 336, 107, 25);		
		btnDeconnexion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().deconnexion();
				}
		});
		contentPane.add(btnDeconnexion);
		
		JButton btnAjouterContact = new JButton("Ajouter Contact");
		btnAjouterContact.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().ajouterContact();
			}
		});
		btnAjouterContact.setFont(new Font("Dialog", Font.BOLD, 11));
		btnAjouterContact.setBounds(12, 335, 158, 25);
		contentPane.add(btnAjouterContact);
		
		JLabel lblVoustesConnect = new JLabel("Votre nom de compte :");
		lblVoustesConnect.setHorizontalAlignment(SwingConstants.RIGHT);
		lblVoustesConnect.setBounds(188, 17, 147, 15);
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(182, 237, 343, 86);
		scrollPane_1.setLayout(null);
		contentPane.add(scrollPane_1);
		

		
		JLabel lblpseudo;
		lblpseudo = new JLabel("Nom de compte :");
		lblpseudo.setHorizontalAlignment(SwingConstants.RIGHT);
		lblpseudo.setBounds(188, 17, 107, 15);
		contentPane.add(lblpseudo);
		
		JLabel lblOrganisme_1 = new JLabel("Organisme :");
		lblOrganisme_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblOrganisme_1.setBounds(220, 44, 75, 15);
		contentPane.add(lblOrganisme_1);
		
		JLabel lblStatut = new JLabel("Statut :");
		lblStatut.setHorizontalAlignment(SwingConstants.RIGHT);
		lblStatut.setBounds(251, 71, 44, 15);
		contentPane.add(lblStatut);
		
		rdbtnDispo = new JRadioButton("Disponible");
		rdbtnDispo.setBounds(309, 66, 102, 25);
		contentPane.add(rdbtnDispo);
		rdbtnDispo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Object source = e.getSource();
				if (source == rdbtnDispo) {
					getControleur().changerStatut("0");
				}
			}
		});
		
		rdbtnOccupe = new JRadioButton("Occupe");
		rdbtnOccupe.setBounds(421, 67, 91, 23);
		contentPane.add(rdbtnOccupe);
		rdbtnOccupe.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Object source = e.getSource();
				if (source == rdbtnOccupe) {
					getControleur().changerStatut("1");
				}
			}
		});
		
		
		//On lie les boutons radio pour ne pouvoir n'en sélectionner qu'un seul et on met basique par défaut.
	    g.add(rdbtnDispo);
	    g.add(rdbtnOccupe);
	    
	    
		textFieldPseudo = new JTextField();
		textFieldPseudo.setEditable(false);
		textFieldPseudo.setBounds(398, 15, 114, 19);
		contentPane.add(textFieldPseudo);
		textFieldPseudo.setColumns(10);
		
		textFieldOrganismeLoc = new JTextField();
		textFieldOrganismeLoc.setEditable(false);
		textFieldOrganismeLoc.setColumns(10);
		textFieldOrganismeLoc.setBounds(398, 44, 114, 19);
		contentPane.add(textFieldOrganismeLoc);
		
		JButton btnActualiser = new JButton("Actualiser");
		btnActualiser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().actuCont();
			}
		});
		btnActualiser.setBounds(12, 298, 158, 25);
		contentPane.add(btnActualiser);
		
		getControleur().infProfil(getControleur().getProfil());
		

	}

	public void alimente() {
		// TODO
		if (controleur.getStatutCompte().equals("0")) {
			rdbtnDispo.setSelected(true);
		}
		else {
			rdbtnOccupe.setSelected(true);
		}
		Vector<String> vec = controleur.getVec();
		if (vec.size() != 0) {
			for (int i = 0; i < vec.size(); i++) {
				modelCo.addElement(vec.get(i));
			}
		} else {
			modelCo.addElement("Aucun Contact connecté");
		}

		// listCo.getModel().setForeground(Color.gray);
		// for (String mapKey : ouv.keySet()) {
		// String num = ouv.get(mapKey).getIsbn();
		// vec.add(num);
		// }

		// for(int i = vec.size()-1 ; i >= 0 ; i--)
		// {
		// model.addElement(vec.elementAt(i));
		// }
	}

	public void compte(String[] s) {
		textFieldPseudo.setText(s[0]);
		textFieldOrganismeLoc.setText(s[1]);
		// textFieldStatutLoc.setText(s[2]);

	}

	public void cont(String[] s) {
		textFieldNCompte.setText(s[0]);
		textFieldOrg.setText(s[1]);
		textFieldLangue.setText(s[2]);
	}
}